﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using eeenno.SpeeedReaderUI.Forms;
using eeenno.ReaderEngine.Utilities;

namespace eeenno.SpeeedReaderUI
{
    static class SpeeedReaderMain
    {

#if Marketplace
        static string m_certModulus = "g26T2qnMKdyfWSaI5JOdzQ7/TRJjFHHr7U+i0KP9SSGK07fmW3B+oatjiQFJwxHrJlLIYkmNiA645cygc1WFtNqfr/O5TRCYt+i4U8UAnkhlClVasccywhRnHEpYqEs/qfcAwtv8o3GURtHC8kWSOFy5YJIqDjwantsjyIrDEiE=";
        static string m_certExponent = "AQAB";
        static string m_appSku = "4cebd73e-d60f-43f5-8e32-40a47981271b";
        [DllImport("coredll.dll")]
        private extern static int GetDeviceUniqueID([In, Out] byte[] appdata, int cbApplictionData, int dwDeviceIDVersion, [In, Out] byte[] deviceIDOuput, out uint pcbDeviceIDOutput);

        [MTAThread]
        static void Main()
        {
            //Pre-Initialization code
            // ....
            byte[] licenseBytes = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Security\\Software\\Microsoft\\Marketplace\\Licenses\\").GetValue(m_appSku) as byte[];
            string data = m_appSku.ToLower() + " " + GetUniqueDeviceID();
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            //Create a new instance of RSAParameters.
            RSAParameters RSAKeyInfo = new RSAParameters();
            //Set RSAKeyInfo to the public key values. 
            RSAKeyInfo.Modulus = Convert.FromBase64String(m_certModulus);
            RSAKeyInfo.Exponent = Convert.FromBase64String(m_certExponent);
            rsa.ImportParameters(RSAKeyInfo);
            byte[] bData = System.Text.Encoding.ASCII.GetBytes(data);
            if (null == licenseBytes || !rsa.VerifyData(bData, new SHA1CryptoServiceProvider(), licenseBytes))
            {
                MessageBox.Show("Could not verify signature");
            }
            else
            {
                //MessageBox.Show("License is valid!");
                try
                {
                    Application.Run(new MainForm());
                }
                catch (Exception e)
                {
                    MainForm.DisplayMessageBox("Something serious happened. I think I need to shutdown now. Sorry.");
                    LogManager.LogCrash("SpeeedReaderMain.Main", e.ToString() + ": " + e.Message); 
                }
            }
            // ...
            // Post-initialization code            
        }
        public static string GetUniqueDeviceID()
        {
            // Call the GetDeviceUniqueID
            byte[] appData = Encoding.Unicode.GetBytes("Marketplace");
            int appDataSize = appData.Length;
            byte[] outData = new byte[20];
            uint outSize = 20;
            GetDeviceUniqueID(appData, appDataSize, 1, outData, out outSize);
            StringBuilder resultSB = new StringBuilder();
            foreach (byte b in outData)
            {
                resultSB.Append(string.Format("{0:x2}", b));
            }
            return resultSB.ToString();
        }
#else
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main()
        {
            try
            {
                Application.Run(new MainForm());
            }
            catch (Exception e)
            {
                MainForm.DisplayMessageBox("Something serious happened. I think I need to shutdown now. Sorry.");
                LogManager.LogCrash("SpeeedReaderMain.Main", e.ToString() + ": " + e.Message);
            }
        }
#endif
    }
}